import { request } from "../request";

const token = localStorage.getItem("token");

/**
 * 上传文件接口返回结构
 */
export interface UploadFileRes {
    code: number;
    message: string;
    data: Record<string, unknown>;
    timestamp: Record<string, unknown>;
}

/**
 * 上传单个文件
 * @param params 上传参数
 * @returns 上传结果
 */
export const uploadFileAPI = async (params: {
    folder: string;
    file: File;
}): Promise<UploadFileRes> => {
    const { folder, file } = params;
    const token = localStorage.getItem("token");
    const formData = new FormData();
    formData.append("folder", folder);
    formData.append("file", file);

    return request("/api/file/upload", {
        method: "post",
        data: formData,
        headers: {
            Authorization: `Bearer ${token}`
            // Content-Type 不要手动设置，axios 会自动识别 FormData
        }
    });
};
